//
//  WhyViewController.m
//  ASI框架使用
//
//  Created by Maggie on 14-9-8.
//  Copyright (c) 2014年 fmning. All rights reserved.
//

#import "WhyViewController.h"

@interface WhyViewController ()

@end

@implementation WhyViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    
    //测试异步请求
//    [self sendASynchronousRequest];
    [self sendPostRequest];
//    [self sendSynchronousRequest];
    NSLog(@"test");
    
    
    
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark - 发送同步请求
-(void)sendSynchronousRequest
{
    int id = 2;
    
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"http://localhost:8888/job/detail.php?jobid=%d",id]];
    
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    //启动同步请求
    [request startSynchronous];
    
    if (request.error == nil) {
        //responseString直接接收返回的响应字符串
//        NSString *result = [request responseString];
        
        //responseData返回字节流
        NSData *data = [request responseData];
        
        NSArray *list = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"%@",list);
    }
    else{
        NSLog(@"fail!%@",request.error.description);
    }
}
#pragma mark - 发送异步请求
-(void)sendASynchronousRequest
{
    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/job/jobs.php"];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    
    request.delegate = self;
    
    [request startAsynchronous];
}

#pragma mark - ASIHTTPRequest异步请求委托方法
-(void)requestFinished:(ASIHTTPRequest *)request{
    //获得请求的结果
    NSData *data = [request responseData];
    NSArray *list = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
    NSLog(@"%@",list);
    
}

#pragma mark - 请求失败
-(void)requestFailed:(ASIHTTPRequest *)request{
    NSLog(@"请求失败！");
}

#pragma mark - 发送POST请求
-(void)sendPostRequest
{
    NSString *username = @"Fui";
    NSString *password = @"1234560";
    
    NSURL *url = [NSURL URLWithString:@"http://localhost:8888/jobsystem/index.php/home/user/login"];
    
    ASIFormDataRequest *form = [ASIFormDataRequest requestWithURL:url];
    
    [form setPostValue:username forKey:@"username"];
    [form setPostValue:password forKey:@"password"];
    
    [form setRequestMethod:@"POST"];
    
    //发送同步请求
    [form startSynchronous];
    
    if (form.error == nil) {
        //responseString直接接收返回的响应字符串
        NSString *result = [form responseString];
        NSLog(@"%@",result);
        
        //responseData返回字节流
//        NSData *data = [form responseData];
    }
    else{
        NSLog(@"请求失败!%@",form.error.description);
    }
}



@end
